package com.zr.Inquiry.controller;

import com.zr.Inquiry.meiju.CurrencyCodeEnum;
import com.zr.Inquiry.meiju.InquiryStatusEnum;
import com.zr.Inquiry.meiju.IsTaxEnum;
import com.zr.Inquiry.meiju.TypeEnum;
import com.zr.Inquiry.pojo.*;
import com.zr.Inquiry.pojo.StatusVo;
import com.zr.Inquiry.service.InquiryService;
import com.zr.planclass.meiju.StatusEnum;
import com.zr.planclass.pojo.*;
import com.zr.qianduan.SupplierVo;
import com.zr.util.ResultVo;
import com.zr.util.ResultVoBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/9/24.
 */
@RestController
@CrossOrigin //支持跨域
public class InquiryController {

    @Autowired
    private InquiryService inquiryService;

    @PostMapping("inquiryList/search")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo queryPage(@RequestBody InquirySelectVo inquirySelectVo) {
        ResultVo resultVo = inquiryService.queryPage(inquirySelectVo);
        return resultVo;
    }

    /**
     * 下拉框
     */
    @PostMapping("metadata/getPullDownList")
    public ResultVo queryXiaLa(){
        Map<String,List<StatusVo>> xialaMap = new HashMap<>();

        //状态
        List<StatusVo> statusVoList = new ArrayList<>();
        //遍历枚举
        for (StatusEnum s : StatusEnum.values()) {
            StatusVo statusVo = new StatusVo();
            statusVo.setValue(s.getStatusValue());
            statusVo.setLabel(s.getStatusName());
            statusVoList.add(statusVo);
        }
        xialaMap.put("status",statusVoList);

        //询价类型
        List<StatusVo> typeList = new ArrayList<>();
        //遍历枚举
        for (TypeEnum t : TypeEnum.values()) {
            StatusVo statusVo = new StatusVo();
            statusVo.setLabel(t.getEnumName());
            statusVo.setValue(t.getEnumValue());
            typeList.add(statusVo);
        }
        xialaMap.put("type",typeList);

        //询价状态
        List<StatusVo> inquiryStatusList = new ArrayList<>();
        //遍历枚举
        for (InquiryStatusEnum i : InquiryStatusEnum.values()) {
            StatusVo statusVo = new StatusVo();
            statusVo.setValue(i.getInquiryStatusValue());
            statusVo.setLabel(i.getInquiryStatusName());
            inquiryStatusList.add(statusVo);
        }
        xialaMap.put("inquiryStatus",inquiryStatusList);

        //询价币别
        List<StatusVo> currencyCodeList = new ArrayList<>();
        //遍历枚举
        for (CurrencyCodeEnum c : CurrencyCodeEnum.values()) {
            StatusVo statusVo = new StatusVo();
            statusVo.setValue(c.getCurrencyCodeValue());
            statusVo.setLabel(c.getCurrencyCodeName());
            currencyCodeList.add(statusVo);
        }
        xialaMap.put("currencyCode",currencyCodeList);

        //是否含税
        List<StatusVo> isTaxList = new ArrayList<>();
        //遍历枚举
        for (IsTaxEnum i : IsTaxEnum.values()) {
            StatusVo statusVo = new StatusVo();
            statusVo.setValue(i.getIsTaxValue());
            statusVo.setLabel(i.getIsTaxName());
            isTaxList.add(statusVo);
        }
        xialaMap.put("isTax",isTaxList);
        return ResultVoBuilder.success(xialaMap);
    }

    @GetMapping("partner/queryByLegalPerson")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo queryByLegalPerson(InquiryVo inquiryVo) {
        ResultVo resultVo =  inquiryService.queryByLegalPerson(inquiryVo);
        return resultVo;
    }


    @PostMapping("inquiryList/selectMaterial")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo querySpare(@RequestBody SpareSelectVo spareSelectVo) {
        ResultVo resultVo = inquiryService.querySpare(spareSelectVo);
        return resultVo;
    }

    @PostMapping("material/searchPartnerPage")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo querySupplier(@RequestBody SupplierSelectVo supplierSelectVo) {
        ResultVo resultVo = inquiryService.querySupplier(supplierSelectVo);
        return resultVo;
    }

    @PostMapping("inquiryList/add")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo add(@RequestBody InquiryAddVo inquiryAddVo, BindingResult bindingResult) {
        //判断是否存在错误信息
        if (bindingResult.hasErrors()){
            //如果存在return给页面错误信息
            return ResultVoBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        //如果没有错误跳到service层进行逻辑处理
        return inquiryService.addInquiry(inquiryAddVo);
    }

    @PostMapping("inquirySheet/see")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo query(@RequestBody InquirySelectVo inquirySelectVo, BindingResult bindingResult) {
        //判断是否存在错误信息
        if (bindingResult.hasErrors()){
            //如果存在return给页面错误信息
            return ResultVoBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = inquiryService.query(inquirySelectVo);
        return resultVo;
    }

    @PostMapping("inquiryList/modifyState")//@RequestBody接受json格式，@Valid做表单验证， BindResult接受错误提示信息
    public ResultVo modifyState(@RequestBody InquiryUpdateStatusVo inquiryUpdateStatusVo) {
        ResultVo resultVo = inquiryService.modifyState(inquiryUpdateStatusVo);
        return resultVo;
    }


//    @PostMapping("quotation/InquiryIntroduction")
//    public ResultVo importFile(MultipartFile file)throws Exception{
//        return inquiryService.importFile(file);
//    }

//    @GetMapping("/exportExcel/master")
//    public ResultVo exportExcel(HttpServletResponse resp, String LegalPersonCode, String LegalPlantCode, String LegalPlantDec, Integer Enabled) throws Exception{
//        InquirySelectVo inquirySelectVo = new InquirySelectVo();
//        inquirySelectVo.setLegalPersonCode(LegalPersonCode);
//        inquirySelectVo.setLegalPlantCode(LegalPlantCode);
//        inquirySelectVo.setLegalPlantDec(LegalPlantDec);
//        inquirySelectVo.setEnabled(Enabled);
//        return corporateFactoryService.exportExcel(resp,corporateFactorySelectVo);
//    }

}
